/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package econutri.servicos.recordatorio;

import econutri.util.Utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lomtools.persistence.GenericDAO;

/**
 *
 * @author Leonardo Oliveira Moreira
 * 
 */
public class SalvarRecordatorioNutrienteXMLServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        response.setContentType("application/xml;charset=ISO-8859-1");
        String pacienteCodigo = (request.getParameter("pacienteCodigo") == null || request.getParameter("pacienteCodigo").trim().length() == 0 ? "-1" : request.getParameter("pacienteCodigo"));
        String data = (request.getParameter("data") == null || request.getParameter("data").trim().length() == 0 ? "-1" : request.getParameter("data"));

        String carboidratoMin = (request.getParameter("carboidratoMin") == null || request.getParameter("carboidratoMin").trim().length() == 0 ? "-1" : request.getParameter("carboidratoMin"));
        String proteinaMin = (request.getParameter("proteinaMin") == null || request.getParameter("proteinaMin").trim().length() == 0 ? "-1" : request.getParameter("proteinaMin"));
        String lipidioMin = (request.getParameter("lipidioMin") == null || request.getParameter("lipidioMin").trim().length() == 0 ? "-1" : request.getParameter("lipidioMin"));
        String calcioMin = (request.getParameter("calcioMin") == null || request.getParameter("calcioMin").trim().length() == 0 ? "-1" : request.getParameter("calcioMin"));
        String fosforoMin = (request.getParameter("fosforoMin") == null || request.getParameter("fosforoMin").trim().length() == 0 ? "-1" : request.getParameter("fosforoMin"));
        String ferroMin = (request.getParameter("ferroMin") == null || request.getParameter("ferroMin").trim().length() == 0 ? "-1" : request.getParameter("ferroMin"));
        String sodioMin = (request.getParameter("sodioMin") == null || request.getParameter("sodioMin").trim().length() == 0 ? "-1" : request.getParameter("sodioMin"));
        String vitaminaAMin = (request.getParameter("vitaminaAMin") == null || request.getParameter("vitaminaAMin").trim().length() == 0 ? "-1" : request.getParameter("vitaminaAMin"));
        String vitaminaB1Min = (request.getParameter("vitaminaB1Min") == null || request.getParameter("vitaminaB1Min").trim().length() == 0 ? "-1" : request.getParameter("vitaminaB1Min"));
        String vitaminaB2Min = (request.getParameter("vitaminaB2Min") == null || request.getParameter("vitaminaB2Min").trim().length() == 0 ? "-1" : request.getParameter("vitaminaB2Min"));
        String niacinaMin = (request.getParameter("niacinaMin") == null || request.getParameter("niacinaMin").trim().length() == 0 ? "-1" : request.getParameter("niacinaMin"));
        String vitaminaCMin = (request.getParameter("vitaminaCMin") == null || request.getParameter("vitaminaCMin").trim().length() == 0 ? "-1" : request.getParameter("vitaminaCMin"));
        String fibraMin = (request.getParameter("fibraMin") == null || request.getParameter("fibraMin").trim().length() == 0 ? "-1" : request.getParameter("fibraMin"));
        String colesterolMin = (request.getParameter("colesterolMin") == null || request.getParameter("colesterolMin").trim().length() == 0 ? "-1" : request.getParameter("colesterolMin"));

        String carboidratoMax = (request.getParameter("carboidratoMax") == null || request.getParameter("carboidratoMax").trim().length() == 0 ? "-1" : request.getParameter("carboidratoMax"));
        String proteinaMax = (request.getParameter("proteinaMax") == null || request.getParameter("proteinaMax").trim().length() == 0 ? "-1" : request.getParameter("proteinaMax"));
        String lipidioMax = (request.getParameter("lipidioMax") == null || request.getParameter("lipidioMax").trim().length() == 0 ? "-1" : request.getParameter("lipidioMax"));
        String calcioMax = (request.getParameter("calcioMax") == null || request.getParameter("calcioMax").trim().length() == 0 ? "-1" : request.getParameter("calcioMax"));
        String fosforoMax = (request.getParameter("fosforoMax") == null || request.getParameter("fosforoMax").trim().length() == 0 ? "-1" : request.getParameter("fosforoMax"));
        String ferroMax = (request.getParameter("ferroMax") == null || request.getParameter("ferroMax").trim().length() == 0 ? "-1" : request.getParameter("ferroMax"));
        String sodioMax = (request.getParameter("sodioMax") == null || request.getParameter("sodioMax").trim().length() == 0 ? "-1" : request.getParameter("sodioMax"));
        String vitaminaAMax = (request.getParameter("vitaminaAMax") == null || request.getParameter("vitaminaAMax").trim().length() == 0 ? "-1" : request.getParameter("vitaminaAMax"));
        String vitaminaB1Max = (request.getParameter("vitaminaB1Max") == null || request.getParameter("vitaminaB1Max").trim().length() == 0 ? "-1" : request.getParameter("vitaminaB1Max"));
        String vitaminaB2Max = (request.getParameter("vitaminaB2Max") == null || request.getParameter("vitaminaB2Max").trim().length() == 0 ? "-1" : request.getParameter("vitaminaB2Max"));
        String niacinaMax = (request.getParameter("niacinaMax") == null || request.getParameter("niacinaMax").trim().length() == 0 ? "-1" : request.getParameter("niacinaMax"));
        String vitaminaCMax = (request.getParameter("vitaminaCMax") == null || request.getParameter("vitaminaCMax").trim().length() == 0 ? "-1" : request.getParameter("vitaminaCMax"));
        String fibraMax = (request.getParameter("fibraMax") == null || request.getParameter("fibraMax").trim().length() == 0 ? "-1" : request.getParameter("fibraMax"));
        String colesterolMax = (request.getParameter("colesterolMax") == null || request.getParameter("colesterolMax").trim().length() == 0 ? "-1" : request.getParameter("colesterolMax"));

        String xml = "";
        xml += "<result>";
        try {
            GenericDAO dao = new GenericDAO();
            if (pacienteCodigo.equalsIgnoreCase("-1") || data.equalsIgnoreCase("-1") ||
                carboidratoMin.equalsIgnoreCase("-1") || proteinaMin.equalsIgnoreCase("-1") ||
                lipidioMin.equalsIgnoreCase("-1") || calcioMin.equalsIgnoreCase("-1") ||
                fosforoMin.equalsIgnoreCase("-1") || ferroMin.equalsIgnoreCase("-1") ||
                sodioMin.equalsIgnoreCase("-1") || vitaminaAMin.equalsIgnoreCase("-1") ||
                vitaminaB1Min.equalsIgnoreCase("-1") || vitaminaB2Min.equalsIgnoreCase("-1") ||
                niacinaMin.equalsIgnoreCase("-1") || vitaminaCMin.equalsIgnoreCase("-1") ||
                fibraMin.equalsIgnoreCase("-1") || colesterolMin.equalsIgnoreCase("-1") ||
                carboidratoMax.equalsIgnoreCase("-1") || proteinaMax.equalsIgnoreCase("-1") ||
                lipidioMax.equalsIgnoreCase("-1") || calcioMax.equalsIgnoreCase("-1") ||
                fosforoMax.equalsIgnoreCase("-1") || ferroMax.equalsIgnoreCase("-1") ||
                sodioMax.equalsIgnoreCase("-1") || vitaminaAMax.equalsIgnoreCase("-1") ||
                vitaminaB1Max.equalsIgnoreCase("-1") || vitaminaB2Max.equalsIgnoreCase("-1") ||
                niacinaMax.equalsIgnoreCase("-1") || vitaminaCMax.equalsIgnoreCase("-1") ||
                fibraMax.equalsIgnoreCase("-1") || colesterolMax.equalsIgnoreCase("-1")) {
                throw new SQLException();
            }
            String sql = "";
            sql = "SELECT * FROM recordatorionutriente WHERE pacientecodigo = " + pacienteCodigo + " AND data = '" + Utils.converterDataSQL(data) + "'";
            ResultSet rs = dao.executeQuery(sql);
            boolean achou = false;
            while (rs != null && rs.next()) {
                achou = true;
            }
            if (! achou) {
                sql = "" +
                "INSERT INTO recordatorionutriente " +
                "(" +
                "pacientecodigo, data, " +
                "carboidratomin, proteinamin, " +
                "lipidiomin, calciomin, fosforomin, ferromin, sodiomin, " +
                "vitaminaamin, vitaminab1min, vitaminab2min, niacinamin, " +
                "vitaminacmin, fibramin, colesterolmin, carboidratomax, " +
                "proteinamax, lipidiomax, calciomax, fosforomax, " +
                "ferromax, sodiomax, vitaminaamax, vitaminab1max, " +
                "vitaminab2max, niacinamax, vitaminacmax, fibramax, " +
                "colesterolmax" +
                ") " +
                "VALUES " +
                "(" + 
                pacienteCodigo + ",'" + Utils.converterDataSQL(data) + "'," +
                carboidratoMin + ", " + proteinaMin + ", " +
                lipidioMin + ", " + calcioMin + ", " + fosforoMin + ", " + ferroMin + ", " + sodioMin + ", " +
                vitaminaAMin + ", " + vitaminaB1Min + ", " + vitaminaB2Min + ", " + niacinaMin + ", " +
                vitaminaCMin + ", " + fibraMin+ ", " + colesterolMin + ", " + carboidratoMax + ", " +
                proteinaMax + ", " + lipidioMax + ", " + calcioMax + ", " + fosforoMax + ", " +
                ferroMax + ", " + sodioMax + ", " + vitaminaAMax + ", " + vitaminaB1Max + ", " +
                vitaminaB2Max + ", " + niacinaMax + ", " + vitaminaCMax + ", " + fibraMax + ", " +
                colesterolMax +
                ")";
                dao.insert(sql);
            }
            else {
                sql = "" +
                "UPDATE recordatorionutriente SET " +
                "carboidratomin = " + carboidratoMin + ", " +
                "proteinamin = " + proteinaMin + ", " +
                "lipidiomin = " + lipidioMin + ", " +
                "calciomin = " + calcioMin + ", " +
                "fosforomin = " + fosforoMin + ", " +
                "ferromin = " + ferroMin + ", " +
                "sodiomin = " + sodioMin + ", " +
                "vitaminaamin = " + vitaminaAMin + ", " +
                "vitaminab1min = " + vitaminaB1Min + ", " +
                "vitaminab2min = " + vitaminaB2Min + ", " +
                "niacinamin = " + niacinaMin + ", " +
                "vitaminacmin = " + vitaminaCMin + ", " +
                "fibramin = " + fibraMin + ", " +
                "colesterolmin = " + colesterolMin + ", " + 
                "carboidratomax = " + carboidratoMax + ", " +
                "proteinamax = " + proteinaMax + ", " +
                "lipidiomax = " + lipidioMax + ", " +
                "calciomax = " + calcioMax + ", " +
                "fosforomax = " + fosforoMax + ", " +
                "ferromax = " + ferroMax + ", " +
                "sodiomax = " + sodioMax + ", " +
                "vitaminaamax = " + vitaminaAMax + ", " +
                "vitaminab1max = " + vitaminaB1Max + ", " +
                "vitaminab2max = " + vitaminaB2Max + ", " +
                "niacinamax = " + niacinaMax + ", " +
                "vitaminacmax = " + vitaminaCMax + ", " +
                "fibramax = " + fibraMax + ", " +
                "colesterolmax = " + colesterolMax + " " +
                "WHERE pacienteCodigo = " + pacienteCodigo + " AND data = '" + Utils.converterDataSQL(data) + "'";
                dao.update(sql);
            }
            dao.close();
            xml += "<acao>Sucesso</acao>";
        } catch (SQLException ex) {
            xml += "<acao>Fracasso</acao>";
            ex.printStackTrace();
        }
        xml += "</result>";
        PrintWriter out = response.getWriter();
        try {
            out.println(xml);
        } finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
